package opl.dal;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import opl.entities.NotificationDetails;
import opl.tables.NotificationTable;

public class DataAccess {

	private Context _currentContext;
	private DbHelper _dbHelper;

	public DataAccess(Context context) {
		_currentContext = context;
		_dbHelper = DbHelper.createInstance(_currentContext);
	}

	public List<NotificationDetails> getNotifications() {
		List<NotificationDetails> lstNotifications = new ArrayList<NotificationDetails>();
		Cursor cur = _dbHelper.select("1=1", NotificationTable.class);

		for (cur.moveToFirst(); !cur.isAfterLast(); cur.moveToNext()) {
			NotificationDetails detail = new NotificationDetails();

			detail.setId(cur.getInt(0));
			detail.setCaption(cur.getString(1));
			detail.setDescription(cur.getString(2));

			lstNotifications.add(detail);
		}

		return lstNotifications;
	}
	//TODO :Joy - Seems not a good approach.Later refactor.
public NotificationDetails getLatestNotificationDetails(){
	Cursor cur = _dbHelper.select("1=1","Id","1", NotificationTable.class);
	NotificationDetails detail=null;
	if (cur.moveToLast()) {
		detail = new NotificationDetails();
		detail.setId(cur.getInt(0));
		detail.setCaption(cur.getString(1));
		detail.setDescription(cur.getString(2));
	}
	return detail;
	
}
	public void addNotification(NotificationDetails detail) {
		ContentValues cv = new ContentValues();

		cv.put(NotificationTable.FIELD_ID, detail.getId());
		cv.put(NotificationTable.FIELD_CAPTION, detail.getCaption());
		cv.put(NotificationTable.FIELD_DESCRIPTION, detail.getDescription());

		_dbHelper.insert(cv, NotificationTable.class);
	}

	public void close() {
		if (_dbHelper != null) {
			_dbHelper.close();
		}
	}

}
